Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe html support to arrays of translations #32361

Merged
merged 1 commit into from
Jul 3, 2018

Conversation

ph3t
Copy link

@ph3t ph3t commented Mar 27, 2018

Summary

The translate method ignores the _html suffix, which leads developers to overuse html_safe in places where it shouldn't be used. This PR adds safe html support to arrays of translations whose key includes the _html suffix.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@ph3t ph3t force-pushed the safe-html-translation-arrays branch 3 times, most recently from 1734cc0 to 98c1aae Compare March 27, 2018 22:13
Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a little hard to grok, do you have a before/after from your app? Or can you make one?

@@ -85,8 +85,11 @@ def translate(key, options = {})
end
end
translation = I18n.translate(scope_key_by_partial(key), html_safe_options.merge(raise: i18n_raise))

translation.respond_to?(:html_safe) ? translation.html_safe : translation
if translation.is_a?(Array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I18n.translate return enumerables other than just Array? E.g. would respond_to?(:map) be clearer/better?

@@ -164,7 +164,8 @@ def test_translate_with_html_count
assert_equal "<a>Other &lt;One&gt;</a>", translate(:'translations.count_html', count: "<One>")
end

def test_translation_returning_an_array_ignores_html_suffix
def test_translate_marks_array_of_translations_with_a_html_safe_suffix_as_safe_html
assert_equal true, translate(:'translations.array_html').all? { |element| element.html_safe? }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally prefer Ruby's definition of true and wouldn't explicitly assert it as the return value.

Think this would be clearer as:

translate(:'translations.array_html').tap do |translated|
  assert_equal %w( foo bar ), translated
  assert translated.all?(&:html_safe?)
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other tests I've seen on this file use assert_equal true so I supposed it was preferred. I will update it.

@ph3t
Copy link
Author

ph3t commented Apr 2, 2018

Having a locale like this:

en:
  hello_html: "<i>Hello world</i>"
  foo_html:
    - "One"
    - "<strong>Two</strong>"
    - "Three &#128075; &#128578;"

And a view:

<h1><%= t('hello_html') %></h1>
<% t('foo_html').each do |translation| %>
  <li><%= translation %></li>
<% end %>

As a developer I would expect both translations to have the same result, the html shouldn't be escaped as it's marked as safe with the _html suffix. But this is what it looks like before adding my code:
image
I would need to explicitly mark translation as html safe in the view using sanitize, html_safe, <%== or raw to display the content correctly.

With the new code, using the _html suffix would be enough:
image

@ph3t
Copy link
Author

ph3t commented May 24, 2018

Hello @kaspth, could you take another look at this?

@kaspth
Copy link
Contributor

kaspth commented Jul 2, 2018

Nice! Needs a squash down to 1 commit. Then a changelog entry. We probably don't need the changes in the .sqlite file.

@ph3t ph3t force-pushed the safe-html-translation-arrays branch from 2c3c30e to 42c3537 Compare July 3, 2018 15:51
@kaspth kaspth merged commit a3a876f into rails:master Jul 3, 2018
@kaspth
Copy link
Contributor

kaspth commented Jul 3, 2018

Great, thanks!

@ph3t ph3t deleted the safe-html-translation-arrays branch July 4, 2018 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants